package com.zhaofan.other;

import org.junit.Test;

public class No153 {

    @Test
    public void test() {
        int[] a = {4, 5, 6, 7, 0, 1, 2};
        System.out.println(findMin2(a));
    }

    public int findMin(int[] nums) {
        int length, result = 0;

        if ((length = nums.length) == 1) {
            return nums[0];
        }
        for (int i = 1; i < length; i++) {
            if (nums[i] < nums[i - 1]) {
                result = nums[i];
            }
        }

        return result;
    }

    public int findMin2(int[] nums) {
        int min = 0, max = nums.length - 1, result = 0;
        while (min < max) {
            int mid = min + (max - min) / 2;
            if (nums[mid] < nums[max]) {
                max = mid;
            } else {
                min = mid + 1;
            }
        }
        return nums[max];
    }
}
